<!-- 编辑子账号 -->
<template>
  <div id="subAccountCreat">
    <c-title :text="status?'编辑账号':'创建账号'"></c-title>
    <div class="flex-a-c" style="margin: 1rem" v-if="status">
      修改密码<van-switch v-model="checked" size="24px" style="margin: 0 0.5rem"/>
    </div>
    <van-form @submit="onSubmit" >
      <van-cell-group inset style="margin-top: 1rem;text-align: left;">
        <van-field v-if="!checked" v-model="account" name='account' left-icon="user-circle-o" placeholder="请输入您的账号" :rules="[{ required: true }]"/>
        <van-field v-if="!status||checked" v-model="pwd" name='pwd' left-icon="closed-eye" :placeholder="status?'请输入旧密码':'请输入您的密码'" :rules="[{ required: true }]"/>
        <van-field v-if="!status||checked" v-model="confirm_pwd" name='confirm_pwd' left-icon="closed-eye" :placeholder="status?'请输入新密码':'请再次确认密码'" :rules="[{ required: true }]"/>
        <van-field v-model="invite_code" name='invite_code' left-icon="friends-o" placeholder="请输入您的邀请码" v-if="!status" :rules="[{ required: true }]"/>
      </van-cell-group>
      <div style="margin: 16px;">
        <van-button round block type="info" native-type="submit">{{status?'保存编辑':'创建子账号'}}</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      checked: false,
      status: false,
      account: '',
      pwd: '',
      confirm_pwd: '',
      invite_code: '',
    };
  },

  created() {
    this.status = Number(this.$route.params.status) === 0? false: true;
    if(this.$route.query.account) this.account = this.$route.query.account;
    if(this.$route.query.invite_code) this.invite_code = this.$route.query.invite_code;
  },

  components: {},

  computed: {},

  mounted() {},

  methods: {
    onSubmit(values) {
      let _values = values;
      let url = 'plugin.account-fjyx.frontend.sub-account.create';
      if(this.status){
        _values = {
          sid: this.$route.query.sid
        };
        if(!this.checked){
          url = 'plugin.account-fjyx.frontend.sub-account.edit';
          _values.account = values.account;
        } else {
          url = 'plugin.account-fjyx.frontend.sub-account.change-pwd';
          _values.old_pwd = values.pwd;
          _values.new_pwd = values.confirm_pwd;
        }
      }
      this.submitData(url,_values);
    },
    async submitData(url,json) {
      let { msg, result } = await $http.get(url, json, ".");
      if (result == 0) {
        return this.$toast(msg);
      }
      this.$router.replace(this.fun.getUrl("subAccount", {}));
    },
  }
};
</script>
<style lang="scss" rel="stylesheet/scss" scoped>
#subAccountCreat{
   ::v-deep .van-field__left-icon .van-icon{
    font-size: 24px;
  }
}
</style>
